package com.flycent.hqhs.ui;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flycent.hqhs.App;
import com.flycent.hqhs.R;
import com.flycent.hqhs.bean.PageEntity;
import com.flycent.hqhs.bean.Product;
import com.flycent.hqhs.bean.Reviews;
import com.flycent.hqhs.http.WebClient;
import com.flycent.hqhs.http.WebException;
import com.flycent.hqhs.ui.view.AbsViewHolderAdapter;
import com.flycent.hqhs.ui.view.RefreshViewLoadMoreProxy;
import com.flycent.hqhs.ui.view.RefreshViewLoadMoreProxy.OnLoadDataActionListener;
import com.flycent.hqhs.util.StringUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author fbl
 *  待评价商品列表
 */
@SuppressLint("HandlerLeak")
public class EvaluationOrSunSingleActivity extends BackableTitleBarActivity implements OnLoadDataActionListener{

	PullToRefreshListView mRefreshListView;
	RefreshViewLoadMoreProxy mLoadMoreProxy;
	private EvalustionListAdapter mAdapter;
	private List<Reviews> mReviews;
	private int nowPage = 1;
	private RelativeLayout mEmptyRelat;
	private boolean isFirst = true;
	private Handler mHandler = new MyHandler(this);

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_evaluationorsunsingle);
		getTitleBar().setTitle(R.string.title_evaluation_or_sunsingle);
		mEmptyRelat = (RelativeLayout) findViewById(R.id.follow_empty_relat);
		mRefreshListView = (PullToRefreshListView) findViewById(R.id.evaluationorsunsingle_list);
		mRefreshListView.setEmptyView(mEmptyRelat);
		mRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));

		mAdapter = new EvalustionListAdapter(this,R.layout.item_evaluation_or_sunsingle);
		mRefreshListView.setAdapter(mAdapter);

		mLoadMoreProxy = new RefreshViewLoadMoreProxy(mRefreshListView, Mode.BOTH,0,0);
		mLoadMoreProxy.setOnLoadDataActionListener(this);
		mLoadMoreProxy.refreshing();

		mRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent,
					View view, int position, long id) {
				Intent intent = new Intent(EvaluationOrSunSingleActivity.this,ProductDetailActivity.class);
				intent.putExtra("productID", mAdapter.getData().get(position-1).getProduct().getProductId()+"");
				startActivity(intent);
			}
		});

	}

	@Override
	public void onRefresh(int page) {
		updateBrandList(1, true);

	}
	@Override
	public void onLoadMore(int page) {
		nowPage++;
		updateBrandList(nowPage, false);

	}
	private void updateBrandList(final int pageNum,final boolean isRefresh){
		
		new Thread(){
			public void run() {
				try{
					
					WebClient client = WebClient.getInstance(EvaluationOrSunSingleActivity.this);

					PageEntity<Reviews> entry = client.getReviewsList(pageNum, 20, "", "");
					int totlePage = entry.getTotalPages();

					mReviews = entry.getContent();
					if(pageNum > totlePage){
						App.showShortToast(R.string.the_end_page);
						mReviews = null;
					}
				} catch (WebException e) {
					if (WebException.TYPE_SERVER_RETURN_NULL.equals(e.getStatusCode()))
						mReviews = null;
					else
						e.printStackTrace();
				}

				if(isRefresh)
					mHandler.sendEmptyMessage(0);	
				else
					mHandler.sendEmptyMessage(1);
				
			};
		}.start();
		
	}
	
	private class MyHandler extends Handler{
		private WeakReference<Activity> mActivity;
		
		private MyHandler(Activity activity){
			mActivity = new WeakReference<Activity>(activity);
		}
		
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			
			if( mActivity.get() != null ){
				switch (msg.what) {
				case 0:
					mAdapter.update(mReviews);
					mRefreshListView.onRefreshComplete();
					break;
				case 1:
					mAdapter.append(mReviews);
					mRefreshListView.onRefreshComplete();
					break;

				default:
					break;
				}
				
				
			}
		}
		
	}

	private class EvalustionListAdapter extends AbsViewHolderAdapter<Reviews>{

		public EvalustionListAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, final Reviews itemData) {
			ImageView mProImg = getViewFromHolder(R.id.item_evaluation_sunsingle_product_image);
			TextView mProNameText = getViewFromHolder(R.id.item_evaluation_sunsingle_product_name);
			final TextView mProAttributeText = getViewFromHolder(R.id.item_evaluation_sunsingle_attribute_text);
			TextView mStatusText = getViewFromHolder(R.id.evaluation_and_sunsingle_text);
			TextView mpriceText = getViewFromHolder(R.id.item_evaluation_sunsingle_product_price);

			Map<String, String > maps = itemData.getProduct().getValues();

			mProAttributeText.setText(StringUtil.getProductPropertyforMap(maps));    // 商品颜色

			Product mSku = itemData.getProduct();
			ImageLoader.getInstance().displayImage(mSku.getImage(), mProImg);
			mProNameText.setText(mSku.getName());
			mpriceText.setText("￥"+mSku.getPrice());

			//判断button上该显示的
			if( !itemData.isReviewStatus() && !itemData.isSunSingleStatus() ){
				mStatusText.setText("评价/晒单");	
			}else if(!itemData.isReviewStatus() && itemData.isSunSingleStatus()){
				mStatusText.setText("评价");	
			}else if(itemData.isReviewStatus() && !itemData.isSunSingleStatus()){
				mStatusText.setText("晒单");	
			}

			mStatusText.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(EvaluationOrSunSingleActivity.this,EvaluationActivity.class);
					intent.putExtra("skuId", itemData.getProduct().getId());
					intent.putExtra("reviewStatus", itemData.isReviewStatus());
					intent.putExtra("sunSingleStatus", itemData.isSunSingleStatus());
					intent.putExtra("skuName", itemData.getProduct().getName());
					intent.putExtra("skuImage", itemData.getProduct().getImage());
					intent.putExtra("skuPrice", itemData.getProduct().getPrice());
					intent.putExtra("orderId", itemData.getId());
					intent.putExtra("skuAttribute", getText(mProAttributeText));
					startActivity(intent);

				}
			});

		}

	} 

	@Override
	protected void onResume() {
		super.onResume();
		if( !isFirst){
			onRefresh(1);
		}
		isFirst = false;		

	}


}
